CC = cc
CFLAGS = -Wall -Wextra -O2 -std=c99 -D_GNU_SOURCE
LDFLAGS = 

# 目标文件
TARGETS = tcp_server tcp_client tcp_client_bond bond_mode4_optimization

# 默认目标
all: $(TARGETS)

# 编译多线程服务器
tcp_server: tcp_server.c
	$(CC) $(CFLAGS) -o tcp_server tcp_server.c -lpthread $(LDFLAGS)

# 编译多线程客户端
tcp_client: tcp_client.c
	$(CC) $(CFLAGS) -o tcp_client tcp_client.c -lpthread $(LDFLAGS)

# 编译bond网络增强客户端
tcp_client_bond: tcp_client_bond.c
	$(CC) $(CFLAGS) -o tcp_client_bond tcp_client_bond.c -lpthread $(LDFLAGS)

# 编译bond mode=4优化服务器
bond_mode4_optimization: bond_mode4_optimization.c
	$(CC) $(CFLAGS) -o bond_mode4_optimization bond_mode4_optimization.c -lpthread $(LDFLAGS)

# 清理编译文件
clean:
	rm -f $(TARGETS)

# 安装到系统路径
install: $(TARGETS)
	cp tcp_server /usr/local/bin/
	cp tcp_client /usr/local/bin/
	cp tcp_client_bond /usr/local/bin/
	cp bond_mode4_optimization /usr/local/bin/
	chmod +x /usr/local/bin/tcp_server
	chmod +x /usr/local/bin/tcp_client
	chmod +x /usr/local/bin/tcp_client_bond
	chmod +x /usr/local/bin/bond_mode4_optimization

# 卸载
uninstall:
	rm -f /usr/local/bin/tcp_server
	rm -f /usr/local/bin/tcp_client
	rm -f /usr/local/bin/tcp_client_bond
	rm -f /usr/local/bin/bond_mode4_optimization

# 运行测试
test: $(TARGETS)
	@echo "启动多线程服务器..."
	@./tcp_server -p 8888 &
	@sleep 2
	@echo "启动多线程客户端测试..."
	@./tcp_client -s 127.0.0.1 -p 8888 -d 10 -n 4
	@pkill tcp_server

# 帮助信息
help:
	@echo "可用的目标:"
	@echo "  all            - 编译所有程序 (默认)"
	@echo "  tcp_server     - 编译多线程服务器程序"
	@echo "  tcp_client     - 编译多线程客户端程序"
	@echo "  tcp_client_bond - 编译bond网络增强客户端程序"
	@echo "  bond_mode4_optimization - 编译bond mode=4优化服务器"
	@echo "  clean          - 清理编译文件"
	@echo "  install        - 安装到系统路径"
	@echo "  uninstall      - 从系统路径卸载"
	@echo "  test           - 运行本地多线程测试"
	@echo "  help           - 显示此帮助信息"

.PHONY: all clean install uninstall test help
